<template>
  <view class="pay-wrapper">
    <view class="pay-main-info">
      <view class="border price-margin">
        {{ price }}
        <text class="price-danwei">元</text>
      </view>

      <!-- <view class="pay-no" v-if> -->
      <!-- 终端号: <text>{{device_code}}</text> -->
      <!-- </view> -->

      <view class="payType">
        <view class="weui-cells weui-cells_after-title wx-pay-box">
          <!--  #ifdef MP-WEIXIN	  -->
          <image class="wx-pay" src="/static/images/wx-pay.png"></image>
          <!--  #endif -->
          <!--  #ifdef MP-ALIPAY	  -->
          <image class="wx-pay" src="/static/images/ali-pay.png"></image>
          <!--  #endif -->
          <radio-group class="wx-pay-radio">
            <label class="weui-cell weui-check__label">
              <view class="weui-cell__bd">{{ itemsWX.name }}</view>
              <view class="weui-cell__hd">
                <radio :value="itemsWX.value" :checked="true" color="#FF6B03" />
              </view>
            </label>
          </radio-group>
        </view>
      </view>
    </view>
    <!--  #ifdef MP-WEIXIN	  -->
    <button class="submit_pay" @click="clickPay">确定支付</button>
    <!--  #endif -->
    <!--  #ifdef MP-ALIPAY	  -->
    <button class="submit_pay" @click="clickPayAli">确定支付</button>
    <!--  #endif -->
  </view>
</template>

<script>
// pages/payRoad/payRoad.js
let sign = require('../../utils/sign.js');
const Store = require('../../utils/store.js');
const app = getApp();
const Api = app.globalData.Api;
export default {
  data() {
    return {
      user_id: '',
      price: '',
      // #ifdef MP-WEIXIN
      itemsWX: {
        value: 'WX',
        name: '微信支付'
      },
      pay_type: 'WX',
      // #endif
      // #ifdef MP-ALIPAY
      itemsWX: {
        value: 'ZFB',
        name: '支付宝支付'
      },
      // #endif

    };
  },
  onLoad: function (options) {
    // this.setData({
    this.user_id = options.user_id,
      this.price = options.price
    // });
  },
  methods: {
    // TODO区分支付宝跟微信
    clickPayAli() {
      //支付宝支付逻辑
    },
    // 点击支付  
    clickPay: function () {
      let user_id = this.user_id;
      let money = this.price;

      //签名 传值
      const signObject = {
        user_id,
        money
      };
      const signvalue = sign.signutil(signObject);
      app.globalData
        .get(Api.rechargeByWX, {
          user_id,
          money,
          sign: signvalue
        })
        .then((res) => {
          console.log(res);
          // 支付
          uni.requestPayment({
            timeStamp: res.timeStamp,
            nonceStr: res.nonceStr,
            package: res.package,
            signType: res.signType,
            paySign: res.paySign,
            success: function (errmsg) {
              console.log(errmsg);
              //支付成功后执行
              if (errmsg.errMsg == 'requestPayment:ok') {
                uni.showToast({
                  title: '支付成功',
                  icon: 'success'
                });
                uni.reLaunch({
                  url: '../sysUserInfo/sysUserInfo'
                });
              }
            },
            fail: function (res) {
              //console.log(res)
              if (res.errMsg == 'requestPayment:fail cancel') {
                uni.showToast({
                  title: '支付取消',
                  icon: 'none'
                });
              } else {
                uni.showToast({
                  title: res.errmsg,
                  icon: 'none'
                });
              }
            }
          });
        })
        .catch((e) => {
          //console.log(e)
          uni.showModal({
            title: e.message,
            content: ''
          });
        });
    }
  }
};
</script>
<style>
@import "./walletToPay.css";
@import "../../utils/weui.css";
</style>
